package com.zfsmart.component.common.bean.restful.entity;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

@Data
public class RespMsg<T> implements Serializable {
    private static final long serialVersionUID = 4938593393370616885L;
    /**
     * 访问成功
     */
    public static final int RESULT_CODE_200 = 200;
    /**
     * 非法请求，参数错误
     */
    public static final int RESULT_CODE_400 = 400;
    /**
     * 未登录
     */
    public static final int RESULT_CODE_401 = 401;
    /**
     * 用户未实名
     */
    public static final int RESULT_CODE_402 = 402;
    /**
     * 未授权,禁止访问
     */
    public static final int RESULT_CODE_403 = 403;

    /**
     * 找不到访问地址
     */
    public static final int RESULT_CODE_404 = 404;


    /**
     * 系统内部错误
     */
    public static final int RESULT_CODE_500 = 500;

    private int code;
    private String msg;
    private T data;
    private Long timestamp;

    public static <T> RespMsg<T> error(int code, String msg) {
        RespMsg<T> respMsg = new RespMsg();
        if (StringUtils.isEmpty(msg)) {
            msg = "系统繁忙请稍候重试";
        }
        respMsg.setCode(code);
        respMsg.setMsg(msg);
        respMsg.setTimestamp(System.currentTimeMillis());
        return respMsg;
    }

    public static <T> RespMsg<T> success(T data) {
        RespMsg<T> respMsg = new RespMsg();
        respMsg.setCode(RESULT_CODE_200);
        respMsg.setMsg("访问成功");
        respMsg.setTimestamp(System.currentTimeMillis());
        respMsg.setData(data);
        return respMsg;
    }


    public static <T> RespMsg<T> success() {
        RespMsg<T> respMsg = new RespMsg();
        respMsg.setCode(RESULT_CODE_200);
        respMsg.setMsg("访问成功");
        respMsg.setTimestamp(System.currentTimeMillis());
        respMsg.setData(null);
        return respMsg;
    }

}
